﻿using iTFF.WPF.Model.Lyrics.LocalProviders;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using iTFF.WPF.Model.Lyrics;
using iTFF.WPF.CP;

namespace iTFF.Tests
{


    /// <summary>
    ///This is a test class for id3LocalProviderTest and is intended
    ///to contain all id3LocalProviderTest Unit Tests
    ///</summary>
    [TestClass()]
    public class id3LocalProviderTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //    // link with itunes
        //    var cp = ControlPanel.Instance.iTunesControlPanel;
        //}

        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}

        //Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void MyTestInitialize()
        {
            // link with itunes
            var cp = ControlPanel.Instance.iTunesControlPanel;
        }

        //Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            // dispose all
            ControlPanel.Instance.Dispose();
        }

        #endregion


        /// <summary>
        ///A test for IsValid
        ///</summary>
        [TestMethod()]
        public void IsValidTest()
        {
            id3LocalProvider target = new id3LocalProvider();
            Assert.IsTrue(target.IsValid);
        }

        /// <summary>
        ///A test for SaveLyrics
        ///</summary>
        [TestMethod()]
        public void SaveLyricsTest()
        {
            id3LocalProvider target = new id3LocalProvider();

            // first create some lyrics
            var model = TrackConstants.GetSampleLyricsModel();

            Assert.IsTrue(target.SaveLyrics(model, true));
            Assert.IsTrue(target.SaveLyrics(model, true));
            Assert.IsFalse(target.SaveLyrics(model, false));

            Assert.AreEqual(target.GetLyrics(model.iTunesPersistentID).Lyrics, model.Lyrics);
        }

        /// <summary>
        ///A test for RemoveLyrics
        ///</summary>
        [TestMethod()]
        public void RemoveLyricsTest()
        {
            id3LocalProvider target = new id3LocalProvider();

            // first create some lyrics
            var model = TrackConstants.GetSampleLyricsModel();

            // add some lyrics
            Assert.IsTrue(target.SaveLyrics(model, true));

            // delete them
            Assert.IsTrue(target.RemoveLyrics(model.iTunesPersistentID));

            // check if no more existing
            Assert.IsFalse(target.LyricsExists(model.iTunesPersistentID));

            // re-add them to don't loose changes in the track
            Assert.IsTrue(target.SaveLyrics(model, true));
        }

        /// <summary>
        ///A test for LyricsExists
        ///</summary>
        [TestMethod()]
        public void LyricsExistsTest()
        {
            id3LocalProvider target = new id3LocalProvider();

            // first create some lyrics
            var model = TrackConstants.GetSampleLyricsModel();

            // add some lyrics
            Assert.IsTrue(target.SaveLyrics(model, true));

            // check if exist
            Assert.IsTrue(target.LyricsExists(model.iTunesPersistentID));
        }

        /// <summary>
        ///A test for GetLyrics
        ///</summary>
        [TestMethod()]
        public void GetLyricsTest()
        {
            id3LocalProvider target = new id3LocalProvider();

            // first create some lyrics
            var model = TrackConstants.GetSampleLyricsModel();

            // add some lyrics
            Assert.IsTrue(target.SaveLyrics(model, true));

            // get the lyrics
            var lyrics = target.GetLyrics(model.iTunesPersistentID);

            Assert.AreEqual(model.Artist, lyrics.Artist);
            Assert.AreEqual(model.Album, lyrics.Album);
            Assert.AreEqual(model.Name, lyrics.Name);
            Assert.AreEqual(model.iTunesPersistentID, lyrics.iTunesPersistentID);
            Assert.AreEqual(model.Lyrics, lyrics.Lyrics);

            // don't check the provider because we can't save it in itunes
            //Assert.AreEqual(model.Provider.Name, model.Provider.Name);

            // get the lyrics of an invalid track
            Assert.IsNull(target.GetLyrics(0));
        }


        [TestMethod()]
        public void TestAll()
        {
            id3LocalProvider target = new id3LocalProvider();

            // first create some lyrics
            var model = TrackConstants.GetSampleLyricsModel();

            // add some lyrics
            Assert.IsTrue(target.SaveLyrics(model, true));

            // check if exist
            Assert.IsTrue(target.LyricsExists(model.iTunesPersistentID));

            // get the lyrics and verify they are the same
            var lyrics = target.GetLyrics(model.iTunesPersistentID);
            Assert.AreEqual(model.Artist, lyrics.Artist);
            Assert.AreEqual(model.Album, lyrics.Album);
            Assert.AreEqual(model.Name, lyrics.Name);
            Assert.AreEqual(model.iTunesPersistentID, lyrics.iTunesPersistentID);
            Assert.AreEqual(model.Lyrics, lyrics.Lyrics);
            Assert.AreEqual(model.Provider.Name, model.Provider.Name);

            // verify multiple save 
            Assert.IsTrue(target.SaveLyrics(model, true));
            Assert.IsFalse(target.SaveLyrics(model, false));

            // delete them
            Assert.IsTrue(target.RemoveLyrics(model.iTunesPersistentID));

            // check if no more existing
            Assert.IsFalse(target.LyricsExists(model.iTunesPersistentID));

            // add again with force false
            Assert.IsTrue(target.SaveLyrics(model, false));

            // check exist
            Assert.IsTrue(target.LyricsExists(model.iTunesPersistentID));
        }
    }
}
